﻿// Copyright (c) 2006 Antu Corporation。 
// 创建人  ：LinJian
// 创建时间：2006-09-04
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Globalization;

namespace Antu.Business.Business
{
    /// <!--
    /// 功能描述  : 字段操作
    /// 创建人  : LinJian
    /// 创建时间: 2006-09-04
    /// -->
    public class ClsFieldType
    {
        #region 根据字段串值获取字段类型
        /// <summary>
        /// 根据字段串值获取字段类型
        /// </summary>
        /// <param name="strFiledType">字段类型</param>
        /// <returns></returns>
        public static DbType GetDataFieldType(string strFiledType)
        {
            strFiledType = strFiledType.ToUpper();

            if (strFiledType.CompareTo("VARCHAR2") == 0 || strFiledType.CompareTo("CHAR") == 0
                || strFiledType.CompareTo("CLOB") == 0)
            {
                return DbType.String;
            }

            if (strFiledType.CompareTo("DATE") == 0)
            {
                return DbType.Date;
            }

            if (strFiledType.CompareTo("NUMBER") == 0 || strFiledType.CompareTo("LONG") == 0
                || strFiledType.CompareTo("FLOAT") == 0 || strFiledType.CompareTo("INTEGER") == 0)
            {
                return DbType.Decimal;
            }

            if (strFiledType.CompareTo("BLOB") == 0 || strFiledType.CompareTo("LONG RAW") == 0)
            {
                return DbType.Binary;
            }

            return DbType.Object;
        } 
        #endregion

        /// <summary>
        /// 获取制定列的类型
        /// </summary>
        /// <param name="dtColType">存放类类型的内存表</param>
        /// <param name="strColumnName">列名</param>
        /// <returns></returns>
        public static string GetSpecialColType(DataTable dtColType, string strColumnName)
        {
            string strReturn = string.Empty;
            foreach (DataRow drTemp in dtColType.Rows)
            {
                if (drTemp["column_name"].ToString().ToLower() == strColumnName.ToLower())
                {
                    strReturn = drTemp["data_type"].ToString();
                }
            }

            return strReturn;
        }

        /// <summary>
        /// 生成2400个字符，即空格
        /// </summary>
        /// <returns></returns>
        public static string GetSpaceChar()
        {
            StringBuilder sbReturn = new StringBuilder();
            for (int i = 0; i < 2000; i++)
            {
                sbReturn.Append(" ");
            }
            return sbReturn.ToString();
        }

        #region 比较两个数据行，若不相等则用源数据行覆盖目标数据行

        /// <summary>
        /// 比较两个数据行，若不相等则用源数据行覆盖目标数据行
        /// </summary>
        /// <param name="drTarget">目标数据行</param>
        /// <param name="drSource">源数据行</param>
        /// <returns>更新后的数据行</returns>
        public static DataRow UpdateColumns(DataRow drTarget, DataRow drSource)
        {
            DataTable dtTemp = drTarget.Table;
            for (int i = 0; i < dtTemp.Columns.Count; i++)
            {
                string strColumnName = dtTemp.Columns[i].ColumnName;
                if (drSource.Table.Columns.Contains(strColumnName))
                {
                    string strColTypeName = drTarget[strColumnName].GetType().Name;

                    if (strColTypeName.ToUpper() == "DBNULL")
                    {
                        strColTypeName = ClsOracleSystemOperation.GetFieldsNameOfTable(dtTemp.TableName, strColumnName);
                    }

                    if (strColTypeName == "Byte[]" || strColTypeName == "BLOB" || strColTypeName == "LONG RAW")
                    {
                        drTarget[strColumnName] = Convert.FromBase64String(drSource[strColumnName].ToString());
                    }
                    else if (strColTypeName == "CLOB" || strColTypeName == "String")
                    {
                        if (drSource[strColumnName].ToString() != drTarget[strColumnName].ToString())
                        {
                            drTarget[strColumnName] = drSource[strColumnName];

                            int nLength = drTarget[strColumnName].ToString().Length;
                            if (nLength > 2000 && nLength < 4000)
                            {
                                drTarget[strColumnName] += GetSpaceChar();
                            }
                        }
                    }
                    else if (strColTypeName == "DateTime")
                    {
                        if ((drSource[strColumnName].ToString() != "" &&
                            Convert.ToDateTime(drSource[strColumnName].ToString()).ToString() != drTarget[strColumnName].ToString()) ||
                            (drSource[strColumnName].ToString() == "" && drTarget[strColumnName].ToString() != ""))
                        {
                            drTarget[strColumnName] = drSource[strColumnName];
                        }
                    }
                    else
                    {
                        if (drSource[strColumnName].ToString() != drTarget[strColumnName].ToString())
                        {
                            drTarget[strColumnName] = drSource[strColumnName];
                        }
                    }
                }
            }
            return drTarget;
        }

        /// <summary>
        /// 比较两个数据行，若不相等则用源数据行覆盖目标数据行
        /// </summary>
        /// <param name="drTarget">目标数据行</param>
        /// <param name="drSource">源数据行</param>
        /// <param name="dtColType">存放类类型的内存表</param>
        /// <returns>更新后的数据行</returns>
        public static DataRow UpdateColumns(DataRow drTarget, DataRow drSource, DataTable dtColType)
        {
            DataTable dtTemp = drTarget.Table;
            for (int i = 0; i < dtTemp.Columns.Count; i++)
            {
                string strColumnName = dtTemp.Columns[i].ColumnName;
                if (drSource.Table.Columns.Contains(strColumnName))
                {
                    string strTypeName = GetSpecialColType(dtColType, strColumnName);

                    if (strTypeName == "BLOB" || strTypeName == "LONG RAW")
                    {
                        drTarget[strColumnName] = Convert.FromBase64String(drSource[strColumnName].ToString());
                    }
                    else if (strTypeName == "CLOB" || strTypeName == "String")
                    {
                        if (drSource[strColumnName].ToString() != drTarget[strColumnName].ToString())
                        {
                            drTarget[strColumnName] = drSource[strColumnName];

                            int nLength = drTarget[strColumnName].ToString().Length;
                            if (nLength > 2000 && nLength < 4000)
                            {
                                drTarget[strColumnName] += GetSpaceChar();
                            }
                        }
                    }
                    else if (strTypeName == "DATE")
                    {
                        if ((drSource[strColumnName].ToString() != "" &&
                            Convert.ToDateTime(drSource[strColumnName].ToString()).ToString() != drTarget[strColumnName].ToString()) ||
                            (drSource[strColumnName].ToString() == "" && drTarget[strColumnName].ToString() != ""))
                        {
                            drTarget[strColumnName] = drSource[strColumnName];
                        }
                    }
                    else
                    {
                        if (drSource[strColumnName].ToString() != drTarget[strColumnName].ToString())
                        {
                            drTarget[strColumnName] = drSource[strColumnName];
                        }
                    }
                }
            }
            return drTarget;
        } 
        #endregion
    }
}
